//
//  TSExtralItemCollectionView.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 2017/9/11.
//  Copyright © 2017年 dvlproad. All rights reserved.
//

#import "TSExtralItemCollectionView.h"
#import "CQFilesLookCollectionViewCell.h"
#import <CJCollectionViewDataSource/CJExtralItemCollectionViewDataSource.h>

@interface TSExtralItemCollectionView () <UICollectionViewDataSource, UICollectionViewDelegate> {
    
}
@property (nonatomic, strong) CJExtralItemCollectionViewDataSource *equalCellSizeCollectionViewDataSource;

@end


@implementation TSExtralItemCollectionView


/// 初始化方法
- (instancetype)init {
    UICollectionViewLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    if (self) {
        [self setupConfigure];
    }
    return self;
}
- (void)setupConfigure {
    self.backgroundColor = [UIColor clearColor];
    
    CJDataSourceSettingModel *dataSourceSettingModel = [[CJDataSourceSettingModel alloc] init];
    dataSourceSettingModel.maxDataModelShowCount = 5;
    dataSourceSettingModel.extralItemSetting = CJExtralItemSettingTailing;
    
    
    //以下值，可选设置
    //self.allowsMultipleSelection = YES; //是否打开多选
    
    /* 设置Register的Cell或Nib */
    CQFilesLookCollectionViewCell *registerCell = [[CQFilesLookCollectionViewCell alloc] init];
    [self registerClass:[registerCell class] forCellWithReuseIdentifier:@"cell"];
    [self registerClass:[registerCell class] forCellWithReuseIdentifier:@"addCell"];
    
    __weak typeof(self)weakSelf = self;
    
    /* 创建DataSource */
    CJExtralItemCollectionViewDataSource *equalCellSizeCollectionViewDataSource = [[CJExtralItemCollectionViewDataSource alloc] initWithSectionSettingBlock:^CJDataSourceSettingModel * _Nonnull(NSInteger section) {
        if (section == 0) {
            return nil;
        } else {
            CJDataSourceSettingModel *dataSourceSettingModel = [[CJDataSourceSettingModel alloc] init];
            dataSourceSettingModel.maxDataModelShowCount = 1;
            dataSourceSettingModel.extralItemSetting = CJExtralItemSettingTailing;
            return dataSourceSettingModel;
        }
        
    } cellForItemAtIndexPathBlock:^UICollectionViewCell * _Nonnull(UICollectionView * _Nonnull collectionView, NSIndexPath * _Nonnull indexPath, BOOL isExtralItem) {
        if (!isExtralItem) {
            CQFilesLookCollectionViewCell *dataCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
            NSLog(@"dataCell.selected = %@", dataCell.selected ? @"YES" : @"NO");
            [weakSelf __operateDataCell:dataCell withIndexPath:indexPath isSettingOperate:YES];

            return dataCell;
        } else {
            CQFilesLookCollectionViewCell *extralCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"addCell" forIndexPath:indexPath];
            extralCell.cjImageView.image = [UIImage imageNamed:@"cjCollectionViewCellAdd"];
            
            return extralCell;
        }
    }];
    self.equalCellSizeCollectionViewDataSource = equalCellSizeCollectionViewDataSource;
    
    
    
    /* 设置DataSource和Delegate */
    self.dataSource = self.equalCellSizeCollectionViewDataSource;
    self.delegate = self;
}



#pragma mark - Setter
- (void)setDataModels:(NSMutableArray<CQTSNetImageDataModel *> *)dataModels {
    _dataModels = dataModels;
    
    CJTagSectionDataModel *sectionModel = [[CJTagSectionDataModel alloc] init];
    sectionModel.values = dataModels;
    NSMutableArray<CJTagSectionDataModel *> *sectionModels = @[sectionModel];
    self.equalCellSizeCollectionViewDataSource.sectionModels = sectionModels;
}

#pragma mark - Update
/// 更新额外cell的样式即位置，(默认不添加）
- (void)updateExtralItemSetting:(CJExtralItemSetting)extralItemSetting {
    [self.equalCellSizeCollectionViewDataSource updateSectionSettingBlock:^CJDataSourceSettingModel * _Nonnull(NSInteger section) {
        if (section == 0) {
            return nil;
        } else {
            CJDataSourceSettingModel *dataSourceSettingModel = [[CJDataSourceSettingModel alloc] init];
            dataSourceSettingModel.maxDataModelShowCount = 1;
            dataSourceSettingModel.extralItemSetting = extralItemSetting;
            return dataSourceSettingModel;
        }
    }];
}


#pragma mark - UICollectionViewDelegate
// 此点击部分请在子类中实现
////“点到”item时候执行的时间(allowsMultipleSelection为默认的NO的时候，只有选中，而为YES的时候有选中和取消选中两种操作)
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"此点击部分请在子类中实现");
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"此点击部分请在子类中实现");
}


#pragma mark - Private Method
/*
 *  设置或者更新Cell
 *
 *  @param cell             要设置或者更新的Cell
 *  @param indexPath        要设置或者更新的Cell的indexPath
 *  @param isSettingOperate 是否是设置，如果否则为更新
 */
- (void)__operateDataCell:(CQFilesLookCollectionViewCell *)dataCell
                withIndexPath:(NSIndexPath *)indexPath
            isSettingOperate:(BOOL)isSettingOperate
{
    CQTSNetImageDataModel *dataModel = [self.equalCellSizeCollectionViewDataSource dataModelAtIndexPath:indexPath];
    
    if (isSettingOperate) {
        dataCell.cjTextLabel.text = dataModel.name;
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dataModel.imageUrl]]];

            dispatch_async(dispatch_get_main_queue(), ^{
                dataCell.cjImageView.image = image;
            });
        });
        
        dataCell.cjSelectedButton.hidden = NO;
    }
}


@end
